package com.innovation.chengzhangriji.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.innovation.chengzhangriji.ChZhRJApplication;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.BaseRequest;
import com.innovation.chengzhangriji.ui.widget.CuteImageView;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.util.WXUtils;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import java.util.UUID;

public class InviteShareActivity extends AppCompatActivity {

    private String msg;
    //qq分享
    private static Tencent mTencent;
    private final static String mAppid = "" + 1104215150;//正式
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        msg = getIntent().getStringExtra("shareMsg");
        setupLayout();
    }

    private void setupLayout() {
        setContentView(R.layout.activity_invite_share);
        mTencent = Tencent.createInstance(mAppid, InviteShareActivity.this);
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(new CuteToolbar.OnClickCuteToolbarListener() {
            @Override
            public void onClickLeft() {
                finish();
            }

            @Override
            public void onClickRight() {

            }
        });
        CuteImageView imageView = (CuteImageView) findViewById(R.id.image_wechat);
        CuteImageView imageView_qq = (CuteImageView) findViewById(R.id.image_qq);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IWXAPI api = ((ChZhRJApplication) getApplicationContext()).getWeiXinApi();
                String uuid = UUID.randomUUID().toString();
                WXUtils.sendTextToWXSession(api, uuid, msg, msg);
            }
        });
        imageView_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
                sendIntent.setType("text/plain");
                //sendIntent.setPackage("com.tencent.mobileqq");
                // List<ResolveInfo> list= getShareTargets(mContext);
               /* try {
                    Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                    sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
                    if (chooserIntent == null) {
                        return;
                    }
                    mContext.startActivity(chooserIntent);
                } catch (Exception e) {
                    mContext.startActivity(sendIntent);
                }*/
                sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
                startActivity(sendIntent);
//                final Bundle params = new Bundle();
//                params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
//                params.putString(QQShare.SHARE_TO_QQ_TITLE, "成长日记");
//                params.putString(QQShare.SHARE_TO_QQ_SUMMARY, msg);
//                params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://int.chengzhangrj.com/download/download");
//                params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "");
//                mTencent.shareToQQ(InviteShareActivity.this, params, new BaseUiListener());
            }
        });
    }
    private class BaseUiListener implements IUiListener {
        @Override
        public void onComplete(Object response) {
            if (null == response) {
                return;
            }
            JSONObject jsonResponse = (JSONObject) response;
            if (null != jsonResponse && jsonResponse.length() == 0) {
                return;
            }
            doComplete((JSONObject) response);
        }
        protected void doComplete(JSONObject values) {
        }
        @Override
        public void onError(UiError e) {
        }
        @Override
        public void onCancel() {
        }
    }
}
